package com.fetnet.smsalarm;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.telephony.SmsMessage;

import android.app.Notification;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;

public class TextMessageReceiver extends BroadcastReceiver{

	 static boolean vibrate1_on = true;
	 static String KeyWord1 = null;
	 
	 int volume1 = 0;
	 static int currentVolume = 0;
	 static String MsgText = "";
	 static boolean vibrate2_on = true;
	 static String KeyWord2 = null;
	 long[] vibratepattern = {100, 400, 500, 400, 100, 400, 500, 400};
	 int volume2 = 0;
	 static boolean vibrate3_on = true;
	 static String KeyWord3 = null;
	 int volume3 = 0;
	 
	 static int count1 = 0;
	 static int count2 = 0;
	 static int count3 = 0;
	 
	public void onReceive(Context context, Intent intent)
	{
		Bundle bundle=intent.getExtras();
		
		count1 = 0;
		count2 = 0;
		count3 = 0;
		Object[] messages=(Object[])bundle.get("pdus");
		SmsMessage[] sms=new SmsMessage[messages.length];
		
		for(int n=0;n<messages.length;n++){
			sms[n]=SmsMessage.createFromPdu((byte[]) messages[n]);
		}
		try {
			File myFile = new File(Environment.getExternalStorageDirectory().getPath()+"/smsalarm/smsalarm1.cfg");
			FileInputStream fIn = new FileInputStream(myFile);
			BufferedReader myReader = new BufferedReader(
					new InputStreamReader(fIn));
			String aDataRow = "";
			String aBuffer = "";
			while ((aDataRow = myReader.readLine()) != null) {
				aBuffer += aDataRow + "\n";
			}
			String[] txtData = aBuffer.split(",");
			KeyWord1 = txtData[0];
			volume1 = Integer.valueOf(txtData[1]);
            if(KeyWord1.equals(""))
			   KeyWord1 = null;
			if(txtData[2].contains("true")){
				vibrate1_on = true;
			} else {
				vibrate1_on = false;
			}
			myReader.close();
		} catch (Exception e) {
			KeyWord1 = null;
		}
			try {
				File myFile2 = new File(Environment.getExternalStorageDirectory().getPath()+"/smsalarm/smsalarm2.cfg");
				FileInputStream fIn2 = new FileInputStream(myFile2);
				BufferedReader myReader2 = new BufferedReader(
						new InputStreamReader(fIn2));
				String aDataRow2 = "";
				String aBuffer2 = "";
				while ((aDataRow2 = myReader2.readLine()) != null) {
					aBuffer2 += aDataRow2 + "\n";
				}
				String[] txtData2 = aBuffer2.split(",");
				KeyWord2 = txtData2[0];
				volume2 = Integer.valueOf(txtData2[1]);
				if(KeyWord2.equals(""))
				   KeyWord2 = null;
				
				if(txtData2[2].contains("true")){
					vibrate2_on = true;
				} else {
					vibrate2_on = false;
				}
				myReader2.close();
			} catch (Exception e) {
				KeyWord2 = null;
			}
			try {
				File myFile3 = new File(Environment.getExternalStorageDirectory().getPath()+"/smsalarm/smsalarm3.cfg");
				FileInputStream fIn3 = new FileInputStream(myFile3);
				BufferedReader myReader3 = new BufferedReader(
						new InputStreamReader(fIn3));
				String aDataRow3 = "";
				String aBuffer3 = "";
				while ((aDataRow3 = myReader3.readLine()) != null) {
					aBuffer3+= aDataRow3 + "\n";
				}
				String[] txtData3 = aBuffer3.split(",");
				KeyWord3 = txtData3[0];
				volume3 = Integer.valueOf(txtData3[1]);
				if(KeyWord3.equals(""))
				   KeyWord3 = null;
				
				if(txtData3[2].contains("true")){
					vibrate3_on = true;
				} else {
					vibrate3_on = false;
				}
				myReader3.close();
			} catch (Exception e) {
				KeyWord3 = null;
			}
			currentVolume = MainActivity.audio.getStreamVolume(AudioManager.STREAM_MUSIC);
		for(SmsMessage msg:sms){
			String textmsg = msg.getMessageBody();
			
			if(KeyWord1 != null) {
			if(textmsg.contains(KeyWord1)) {
				MsgText = "";
				if(vibrate1_on) {	  
//					Uri alarmSound = Uri.parse("android.resource://com.fetnet.smsalarm/" + R.raw.alarm);
					//currentVolume = MainActivity.audio.getStreamVolume(AudioManager.STREAM_NOTIFICATION);
                    MainActivity.audio.setStreamVolume(AudioManager.STREAM_MUSIC, volume1, 0);
                    
					MyService.notification = new Notification.Builder(context)
			         .setContentTitle("SMS Alarm "+KeyWord1)
			         .setContentText(textmsg)
			         .setSmallIcon(R.drawable.fire_alarm_small)
//			         .setSound(alarmSound)
			         .setVibrate(vibratepattern)
			         .setContentIntent(MyService.appIntent)
			         .build();
                
				} else {

//				Uri alarmSound = Uri.parse("android.resource://com.fetnet.smsalarm/" + R.raw.alarm);
                MainActivity.audio.setStreamVolume(AudioManager.STREAM_MUSIC, volume1, 0);
                MyService.notification = new Notification.Builder(context)
		         .setContentTitle("SMS Alarm "+KeyWord1)
		         .setContentText(textmsg)
		         .setSmallIcon(R.drawable.fire_alarm_small)
		         //.setSound(alarmSound)
		         .setContentIntent(MyService.appIntent)
		         .build();
				}
				MyService.notification.flags |= Notification.FLAG_AUTO_CANCEL;

				MyService.notificationManager.notify(0, MyService.notification);
               
             //   MainActivity.audio.setStreamVolume(AudioManager.STREAM_NOTIFICATION, currentVolume, 0);
				if(!MyService.player1.isPlaying()){  
				MyService.player1 = MediaPlayer.create(context, R.raw.alarm);
                  try {
                	  MyService.player1.prepare();
                  } catch (IllegalStateException e) {
                          // TODO Auto-generated catch block
                          e.printStackTrace();
                  } catch (IOException e) {
                          // TODO Auto-generated catch block
                          e.printStackTrace();
                  }
                  MyService.player1.start();
				}
				count1 = 3;
			}
			
			}
			if(KeyWord2 != null) {
				if(textmsg.contains(KeyWord2)) {
					MsgText = "";
					if(vibrate2_on) {	  
//						Uri alarmSound = Uri.parse("android.resource://com.fetnet.smsalarm/" + R.raw.alarm);
	                    MainActivity.audio.setStreamVolume(AudioManager.STREAM_MUSIC, volume2, 0);

	                    MyService.notification = new Notification.Builder(context)
				         .setContentTitle("SMS Alarm "+KeyWord2)
				         .setContentText(textmsg)
				         .setSmallIcon(R.drawable.fire_alarm_small)
				         //.setSound(alarmSound)
				         .setVibrate(vibratepattern)
				         .setContentIntent(MyService.appIntent)
				         .build();
					} else {

//					Uri alarmSound = Uri.parse("android.resource://com.fetnet.smsalarm/" + R.raw.alarm);
                    MainActivity.audio.setStreamVolume(AudioManager.STREAM_MUSIC, volume2, 0);

                    MyService.notification = new Notification.Builder(context)
			         .setContentTitle("SMS Alarm "+KeyWord2)
			         .setContentText(textmsg)
			         .setSmallIcon(R.drawable.fire_alarm_small)
//			         .setSound(alarmSound)
			         .setContentIntent(MyService.appIntent)
			         .build();
					}
					MyService.notification.flags |= Notification.FLAG_AUTO_CANCEL;

					MyService.notificationManager.notify(0, MyService.notification);
//					 MsgText = textmsg;
					if(MyService.player2.isPlaying()){
					MyService.player2 = MediaPlayer.create(context, R.raw.alarm2);
	                  try {
	                	  MyService.player2.prepare();
	                  } catch (IllegalStateException e) {
	                          // TODO Auto-generated catch block
	                          e.printStackTrace();
	                  } catch (IOException e) {
	                          // TODO Auto-generated catch block
	                          e.printStackTrace();
	                  }
	                  MyService.player2.start();
					}
					count2 = 3;
				}
				
				}
			if(KeyWord3 != null) {
				if(textmsg.contains(KeyWord3)) {
					MsgText = "";
					if(vibrate3_on) {	  
//						Uri alarmSound = Uri.parse("android.resource://com.fetnet.smsalarm/" + R.raw.alarm);
	                    MainActivity.audio.setStreamVolume(AudioManager.STREAM_MUSIC, volume3, 0);

	                    MyService.notification = new Notification.Builder(context)
				         .setContentTitle("SMS Alarm "+KeyWord3)
				         .setContentText(textmsg)
				         .setSmallIcon(R.drawable.fire_alarm_small)
				         //.setSound(alarmSound)
				         .setVibrate(vibratepattern)
				         .setContentIntent(MyService.appIntent)
				         .build();
					} else {

//					Uri alarmSound = Uri.parse("android.resource://com.fetnet.smsalarm/" + R.raw.alarm);
                    MainActivity.audio.setStreamVolume(AudioManager.STREAM_MUSIC, volume3, 0);

                    MyService.notification = new Notification.Builder(context)
			         .setContentTitle("SMS Alarm "+KeyWord3)
			         .setContentText(textmsg)
			         .setSmallIcon(R.drawable.fire_alarm_small)
//			         .setSound(alarmSound)
			         .setContentIntent(MyService.appIntent)
			         .build();
					}
					MyService.notification.flags |= Notification.FLAG_AUTO_CANCEL;

					MyService.notificationManager.notify(0, MyService.notification);
//					 MsgText = textmsg;
					if(!MyService.player3.isPlaying()){
					MyService.player3 = MediaPlayer.create(context, R.raw.alarm3);
	                  try {
	                	  MyService.player3.prepare();
	                  } catch (IllegalStateException e) {
	                          // TODO Auto-generated catch block
	                          e.printStackTrace();
	                  } catch (IOException e) {
	                          // TODO Auto-generated catch block
	                          e.printStackTrace();
	                  }
	                  MyService.player3.start();
					}     
					  count3 = 3;
				}
				
				}
			 if(count1 != 0){
			    MsgText = MsgText + textmsg;
			    count1--;
			 }
			 if(count2 != 0){
				    MsgText = MsgText + textmsg;
				    count2--;
		     }
			 if(count3 != 0){
				    MsgText = MsgText + textmsg;
				    count3--;
		     }
		}
        
	   

	}
}